import React, { useState, useEffect } from 'react';
import styled from 'styled-components';
import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts';

const ChartContainer = styled.div`
  width: 100%;
  height: 300px;
  background: rgba(10, 25, 41, 0.7);
  border: 1px solid rgba(0, 255, 255, 0.3);
  border-radius: 5px;
  padding: 15px;
  position: relative;
  overflow: hidden;
  margin-top: 0;
  
  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, rgba(0, 255, 255, 0.8), transparent);
  }
  
  &:after {
    content: '';
    position: absolute;
    bottom: 0;
    right: 0;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, transparent, rgba(0, 255, 255, 0.8));
  }
`;

const ChartTitle = styled.div`
  font-size: 18px;
  font-weight: bold;
  color: #fff;
  margin-bottom: 10px;
  text-align: center;
  position: relative;
  
  &:after {
    content: '';
    position: absolute;
    bottom: -6px;
    left: 25%;
    width: 50%;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(0, 255, 255, 0.5), transparent);
  }
`;

interface DataPoint {
  name: string;
  数据1: number;
  数据2: number;
  数据3: number;
}

const generateRandomData = (): DataPoint[] => {
  const data: DataPoint[] = [];
  const categories = ['类别A', '类别B', '类别C', '类别D', '类别E', '类别F'];
  
  categories.forEach(category => {
    data.push({
      name: category,
      数据1: Math.floor(Math.random() * 1000) + 500,
      数据2: Math.floor(Math.random() * 800) + 300,
      数据3: Math.floor(Math.random() * 600) + 200
    });
  });
  
  return data;
};

const BarChartPanel: React.FC = () => {
  const [data, setData] = useState<DataPoint[]>(generateRandomData());
  
  // Update data every 5 seconds
  useEffect(() => {
    const timer = setInterval(() => {
      setData(generateRandomData());
    }, 5000);
    
    return () => {
      clearInterval(timer);
    };
  }, []);
  
  return (
    <ChartContainer>
      <ChartTitle>数据统计</ChartTitle>
      <ResponsiveContainer width="100%" height="90%">
        <BarChart
          data={data}
          margin={{
            top: 10,
            right: 20,
            left: 0,
            bottom: 5,
          }}
        >
          <CartesianGrid strokeDasharray="3 3" stroke="rgba(255,255,255,0.1)" />
          <XAxis dataKey="name" stroke="#00ffff" fontSize={11} />
          <YAxis stroke="#00ffff" fontSize={11} />
          <Tooltip 
            contentStyle={{ 
              backgroundColor: 'rgba(10, 25, 41, 0.9)', 
              border: '1px solid rgba(0, 255, 255, 0.5)',
              color: '#fff',
              fontSize: '12px'
            }} 
          />
          <Legend wrapperStyle={{fontSize: '11px'}} />
          <Bar dataKey="数据1" fill="#8884d8" />
          <Bar dataKey="数据2" fill="#82ca9d" />
          <Bar dataKey="数据3" fill="#ffc658" />
        </BarChart>
      </ResponsiveContainer>
    </ChartContainer>
  );
};

export default BarChartPanel; 